package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.RateAdapter;
import com.eeepay.eeepay_v2.model.RateInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.StatuUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerAddModular;

/**
 * Created by Ching on 2016/3/14.
 * 服务费率界面
 */
public class ServiceRateActivity extends ABBaseActivity implements View.OnClickListener, TitleBar.LeftBtnOnClickListener {

    private Button nextBtn;
    private ListView rateLv;
    private TitleBar titleBar;

    private MerAddModular.ServiceRate[] datas;
    private List<RateInfo> rateList = new ArrayList<>();
    private RateAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_service_rate;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.tb_service_rate);
        nextBtn = getViewById(R.id.btn_service_rate);
        rateLv = getViewById(R.id.lv_service_rate);
    }

    @Override
    protected void eventOnClick() {

        titleBar.setLeftOnClickListener(this);
        nextBtn.setOnClickListener(this);
        adapter = new RateAdapter(this);
        rateList = SPUtils.getList(Constant.KEY.RATE);
        MerAddModular.ServiceRate rate = null;
        if (rateList != null && rateList.size() > 0) {
            RateInfo rateInfo;
            datas = new MerAddModular.ServiceRate[rateList.size()];
            for (int i = 0; i < rateList.size(); i++) {
                rateInfo = rateList.get(i);
                rate = new MerAddModular.ServiceRate();
                rate.serviceName = rateInfo.getServiceName();
                rate.cardType = rateInfo.getCardType();
                rate.holidaysMark = rateInfo.getServiceTime();
                rate.rateType = rateInfo.getRateType();
                rate.serviceId = rateInfo.getServiceId();
                rate.fixedRate = rateInfo.getFixedRate();
                rate.id = rateInfo.getId();
                switch (Integer.parseInt(rateInfo.getRateType())) {
                    case 1:
                        rate.singleNumAmount = rateInfo.getSingle_num_amount();
//                        Log.d("serRate", "1 " + rate.singleNumAmount);
                        break;
                    case 2:
                        rate.rate = rateInfo.getRate();
//                        Log.d("serRate", "2 " + rate.rate);
                        break;
                    case 3:
                        rate.safeLine = rateInfo.getSafe_line();
                        rate.rate = rateInfo.getRate();
                        rate.capping = rateInfo.getCapping();
//                        Log.d("serRate", "3 " + rate.rate + " safeLine " + rate.safeLine
//                                + " capping " + rate.capping);
                        break;
                    case 4:
                        rate.rate = rateInfo.getRate();
                        rate.singleNumAmount = rateInfo.getSingle_num_amount();
                        break;
                    case 5:
                        rate.ladder1Rate = rateInfo.getLdr1Rate();
                        rate.ladder1Max = rateInfo.getLdr1Max();
                        rate.ladder2Rate = rateInfo.getLdr2Rate();
//                        Log.d("serRate", " type = 5 " + rate.ladder1Rate + "%<" + rate.ladder1Max + "<" + rate.ladder2Rate);
                        break;
                }
//                rate.rate = rateInfo.getServiceRate();
                datas[i] = rate;
            }
        }
//        else {//模拟数据
//            datas = new MerAddModular.ServiceRate[4];
//            for (int i = 0; i < 4; i++) {
//                rate = new MerAddModular.ServiceRate();
//                rate.cardType = String.valueOf(i);
//                rate.holidaysMark = String.valueOf(i);
//                rate.rateType = String.valueOf(i + 1);
//                switch (Integer.parseInt(rate.rateType)) {
//                    case 1:
//                        rate.singleNumAmount = "1";
//                        break;
//                    case 2:
//                        rate.rate = "2";
//                        break;
//                    case 3:
//                        rate.safeLine = "0";
//                        rate.rate = "0.38";
//                        rate.capping = "3";
//                        break;
//                    case 4:
//                        rate.rate = "0.40";
//                        rate.singleNumAmount = "4";
//                        break;
//                }
//                rate.serviceName = "提现";
//                rate.serviceId = String.valueOf(i);
//                datas[i] = rate;
//            }
        adapter.addAll(datas);
        rateLv.setAdapter(adapter);
//        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_service_rate://下一步
//                for (int i = 0; i < datas.length; i++) {
//                    if (!Pattern.matches(Constant.PROFIT_SET_REG, datas[i].rate))
//                        showToast("第" + (i + 1) + "项费率设置不符合规则");
//                }
                saveRate();
                finish();
                ScreenSwitch.switchActivity(this, ServiceLimitActivity.class, null, 0);
                break;
        }
    }

    public void saveRate() {
        datas = adapter.getDatas();
        MerAddModular.ServiceRate rate;
        RateInfo rateInfo;
        rateList.clear();
        for (int i = 0; i < datas.length; i++) {
            rate = datas[i];
//            if ("1".equals(rate.fixedRate))
//                continue;
            rateInfo = new RateInfo();
            rateInfo.setCardType(rate.cardType);
            rateInfo.setServiceName(rate.serviceName);
            rateInfo.setServiceTime(rate.holidaysMark);
            rateInfo.setRateType(rate.rateType);
            rateInfo.setServiceId(rate.serviceId);
            rateInfo.setRateType(rate.rateType);
            rateInfo.setFixedRate(rate.fixedRate);
            rateInfo.setId(rate.id);
            Log.d("merSerRate.id", " merSerRate.id:" +  rateInfo.getId());
            switch (Integer.parseInt(rate.rateType)) {
                case 1:
                    rateInfo.setSingle_num_amount(rate.singleNumAmount);
                    break;
                case 2:
                    rateInfo.setRate(rate.rate);
                    break;
                case 3:
                    rateInfo.setRate(rate.rate);
                    rateInfo.setSafe_line(rate.safeLine);
                    rateInfo.setCapping(rate.capping);
                    break;
                case 4:
                    rateInfo.setRate(rate.rate);
                    rateInfo.setSingle_num_amount(rate.singleNumAmount);
                    break;
                case 5:
                    rateInfo.setLdr1Rate(rate.ladder1Rate);
                    rateInfo.setLdr1Max(rate.ladder1Max);
                    rateInfo.setLdr2Rate(rate.ladder2Rate);
                    break;

            }
            Log.d("rate", "提交的费率ID: " + rate.serviceId
                    + " 提交的费率类型: " + rate.serviceType);
            rateList.add(rateInfo);
        }
        SPUtils.saveList(rateList, Constant.KEY.RATE);
        StatuUtils.getInstance().setRate("已完成");
        Intent intent = new Intent();
        intent.putExtra("status", "已完成");
        setResult(RESULT_OK, intent);
    }

    @Override
    public void onLeftClick(View view) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            onLeftClick(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
